/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import Controlador.Bean.Ventas.CotizacionBusqueda;
import Controlador.Service.Ventas.GestionVentas;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Luis Enrique
 */
public class CotizacionMainPanel extends javax.swing.JPanel {

    CotizacionBusquedaTableModel myTableModel;
    SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy");
    ArrayList<CotizacionBusqueda> cotizacionBusq = GestionVentas.getInstance().queryAllCotizacion();
    
    //Formato decimal
    DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
    DecimalFormat df; 
    
    class CotizacionBusquedaTableModel extends AbstractTableModel{
        String [] titles = {"Número de Cotización","Documento de Cliente","Razón Social/Nombre","Fecha","Monto Total","Estado"};

        @Override
        public int getRowCount() {
            return cotizacionBusq.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            CotizacionBusqueda pb = cotizacionBusq.get(rowIndex);
            switch(columnIndex){
                case 0: res = pb.getNumero()+""; break;
                case 1: res = pb.getNumDoc()+"";break;
                case 2: res = (pb.getRazonSocial()!=null)?pb.getRazonSocial():(pb.getApePaterno()+" "+pb.getApeMaterno()+", "+pb.getNombres()); break;
                case 3: res = formatFecha.format(pb.getFecha());break;
                case 4: res = df.format(pb.getTotal()); break;
                case 5: res = pb.getEstado();break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public CotizacionMainPanel() {
        initComponents();
        btnGpTipoPersona.add(rbPJuridica);
        btnGpTipoPersona.add(rbPNatural);
        btnGpTipoDocumento.add(rbRUC);
        btnGpTipoDocumento.add(rbDNI);
        
        Calendar calendar = dpFecha.getMonthView().getCalendar();
        calendar.setTime(new Date());
        dpFecha.getMonthView().setUpperBound(calendar.getTime());
        
        this.simbolos.setDecimalSeparator('.');
        this.df = new DecimalFormat("#.00",simbolos);
        
        RestrictedTextField restricted = new RestrictedTextField(txtRazonSocial);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtApePaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
                
        restricted = new RestrictedTextField(txtApeMaterno);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
        
        restricted = new RestrictedTextField(txtNombres);
        restricted.setLimit(50);
        restricted.setOnlyText(true);
        
        restricted = new RestrictedTextField(txtNumDoc);
        restricted.setLimit(11);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumCotizacion);
        restricted.setLimit(6);
        restricted.setOnlyNums(true);
        
        myTableModel = new CotizacionBusquedaTableModel();
        tblCotizaciones.setModel(myTableModel);
        TableRowSorter<CotizacionBusquedaTableModel> sorter = new TableRowSorter<> ((CotizacionBusquedaTableModel) tblCotizaciones.getModel());
        tblCotizaciones.setRowSorter(sorter);
    }

    public void refreshItemsTable(){
        this.cotizacionBusq = GestionVentas.getInstance().queryAllCotizacion();
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshItemsTable2(ArrayList<CotizacionBusqueda> cotizacionesBusq){
        this.cotizacionBusq = new ArrayList(cotizacionesBusq);
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshDetails(){
        if(rbPJuridica.isSelected()){
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(true);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(false);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(false);
            txtNombres.setText("");
            txtNombres.setEnabled(false);
            btnGpTipoDocumento.clearSelection();
            rbRUC.setEnabled(true);
            rbDNI.setEnabled(false);
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }
        else{
            txtRazonSocial.setText("");
            txtRazonSocial.setEnabled(false);
            txtApePaterno.setText("");
            txtApePaterno.setEnabled(true);
            txtApeMaterno.setText("");
            txtApeMaterno.setEnabled(true);
            txtNombres.setText("");
            txtNombres.setEnabled(true);
            btnGpTipoDocumento.clearSelection();
            rbRUC.setEnabled(true);
            rbDNI.setEnabled(true);            
            txtNumDoc.setText("");
            txtNumDoc.setEnabled(true);
        }    
    }
    
    public void limpiarCriterios(){
        txtNumCotizacion.setText("");
        btnGpTipoPersona.clearSelection();
        txtRazonSocial.setText("");
        txtRazonSocial.setEnabled(true);
        txtApePaterno.setText("");
        txtApePaterno.setEnabled(true);
        txtApeMaterno.setText("");
        txtApeMaterno.setEnabled(true);
        txtNombres.setText("");
        txtNombres.setEnabled(true);
        btnGpTipoDocumento.clearSelection();
        rbRUC.setEnabled(true);
        rbDNI.setEnabled(true);
        txtNumDoc.setText("");
        txtNumDoc.setEnabled(true);
        cmbEstado.setSelectedIndex(0);
        dpFecha.setDate(null);
        dpFechaVen.setDate(null);
    }
    
    public boolean valido(){
        boolean b = true;
        
        if(rbRUC.isSelected())
            if(!txtNumDoc.getText().isEmpty())
                if(txtNumDoc.getText().length()!=11){
                    b = false;
                    JOptionPane.showMessageDialog(this, "Error en Num. de Documento RUC", "Error en los datos", JOptionPane.OK_OPTION);
                }           
        if(rbDNI.isSelected())
            if(!txtNumDoc.getText().isEmpty())
                if(txtNumDoc.getText().length()!=8){
                    b = false;
                    JOptionPane.showMessageDialog(this, "Error en Num. de Documento DNI", "Error en los datos", JOptionPane.OK_OPTION);
                }
                
        return b;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGpTipoPersona = new javax.swing.ButtonGroup();
        btnGpTipoDocumento = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCotizaciones = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        txtNumCotizacion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        rbPJuridica = new javax.swing.JRadioButton();
        rbPNatural = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        txtRazonSocial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtApePaterno = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtApeMaterno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        rbRUC = new javax.swing.JRadioButton();
        rbDNI = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        dpFecha = new org.jdesktop.swingx.JXDatePicker();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        dpFechaVen = new org.jdesktop.swingx.JXDatePicker();
        jLabel11 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        btnRegistrar = new javax.swing.JButton();
        btnAnular = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(784, 549));
        setMinimumSize(new java.awt.Dimension(784, 549));
        setPreferredSize(new java.awt.Dimension(784, 549));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Cotizaciones"));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(764, 305));
        jPanel1.setMinimumSize(new java.awt.Dimension(764, 305));
        jPanel1.setPreferredSize(new java.awt.Dimension(764, 305));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tblCotizaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCotizaciones.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCotizaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCotizacionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblCotizaciones);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        jLabel10.setText("Número de Cotización");

        jLabel1.setText("Tipo de Persona");

        rbPJuridica.setText("P. Jurídica");
        rbPJuridica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPJuridicaActionPerformed(evt);
            }
        });

        rbPNatural.setText("P. Natural");
        rbPNatural.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPNaturalActionPerformed(evt);
            }
        });

        jLabel2.setText("Razón Social");

        jLabel3.setText("Ape. Paterno");

        jLabel4.setText("Ape. Materno");

        jLabel5.setText("Nombres");

        jLabel6.setText("Tipo de Doc.");

        rbRUC.setText("R.U.C.");

        rbDNI.setText("D.N.I.");

        jLabel7.setText("Número de Doc.");

        jLabel8.setText("Estado");

        cmbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar", "Registrado", "Confirmado", "Facturado" }));

        jLabel9.setText("Fecha");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        jLabel11.setText("Fecha de Vencimiento");

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel16.setText("R.U.C. (11 dígitos) y D.N.I. (8 dígitos)");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(40, 40, 40)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtApeMaterno, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(txtRazonSocial, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rbPJuridica)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(rbPNatural))
                            .addComponent(txtApePaterno, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(txtNombres, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addComponent(txtNumCotizacion)))
                    .addComponent(jLabel10))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel16))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmbEstado, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dpFecha, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dpFechaVen, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(rbRUC)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(rbDNI))
                                    .addComponent(txtNumDoc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(btnBuscar)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnLimpiar)))))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel10)
                                .addComponent(txtNumCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(rbPNatural)
                            .addComponent(rbPJuridica)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbDNI)
                            .addComponent(rbRUC))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtApePaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtApeMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dpFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dpFechaVen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnBuscar)
                            .addComponent(btnLimpiar))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        btnAnular.setText("Anular");
        btnAnular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnRegistrar)
                        .addGap(18, 18, 18)
                        .addComponent(btnAnular))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRegistrar)
                    .addComponent(btnAnular))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(61, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        CotizacionRegistrarJDialog cotizacionDialog = new CotizacionRegistrarJDialog(null, true);
        cotizacionDialog.setParentPanel(this);
        cotizacionDialog.setVisible(true);
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void btnAnularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnularActionPerformed
        if(tblCotizaciones.getSelectedRow()!=-1){
            int rowSel = tblCotizaciones.getSelectedRow();
            int confirma = JOptionPane.showConfirmDialog(this,"¿Desea anular la cotización: "+cotizacionBusq.get(rowSel).getIdCotizacion()+"?","Confirmar",2);
            if (JOptionPane.OK_OPTION==confirma){             
                GestionVentas.getInstance().anularCotizacion(cotizacionBusq.get(rowSel).getIdCotizacion());
                JOptionPane.showMessageDialog(this,"Se anuló correctamente la cotización.");
                refreshItemsTable();
            }
        }
        else
            JOptionPane.showMessageDialog(this,"Debe seleccionar un registro de la tabla.");        
    }//GEN-LAST:event_btnAnularActionPerformed

    private void tblCotizacionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCotizacionesMouseClicked
        if(evt.getClickCount()==2){
            int rowSel = tblCotizaciones.getSelectedRow();
            CotizacionVerJDialog verDialog = new CotizacionVerJDialog(null, true);
            verDialog.setCotizacion(cotizacionBusq.get(rowSel));
            verDialog.setVisible(true);
        }
    }//GEN-LAST:event_tblCotizacionesMouseClicked

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiarCriterios();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        if(valido()){
            long numero = (txtNumCotizacion.getText().equals(""))?0:Long.parseLong(txtNumCotizacion.getText());
            String tipo;
            if(rbPJuridica.isSelected())
            tipo = "TPJ";
            else if (rbPNatural.isSelected())
            tipo = "TPN";
            else
            tipo=null;
            String razonSocial = (txtRazonSocial.getText().equals(""))?null:"%"+txtRazonSocial.getText()+"%";
            String apePaterno = (txtApePaterno.getText().equals(""))?null:"%"+txtApePaterno.getText()+"%";
            String apeMaterno = (txtApeMaterno.getText().equals(""))?null:"%"+txtApeMaterno.getText()+"%";
            String nombres = (txtNombres.getText().equals(""))?null:"%"+txtNombres.getText()+"%";
            String tipoDoc;
            if(rbDNI.isSelected())
            tipoDoc = "dni";
            else if (rbRUC.isSelected())
            tipoDoc = "ruc";
            else
            tipoDoc=null;
            String numDoc = (txtNumDoc.getText().equals(""))?null:txtNumDoc.getText();
            String estado;
            if(cmbEstado.getSelectedIndex()==1)
            estado = "Registrado";
            else if (cmbEstado.getSelectedIndex()==2)
            estado = "Confirmado";
            else if (cmbEstado.getSelectedIndex()==3)
            estado = "Facturado";
            else
            estado=null;
            Date fecha = (dpFecha.getDate()==null)?null:dpFecha.getDate();
            Date fechaVencimiento = (dpFechaVen.getDate()==null)?null:dpFechaVen.getDate();

            CotizacionBusqueda cotizacionBusqueda = new CotizacionBusqueda (null,null,tipo,razonSocial,apePaterno,apeMaterno,nombres,tipoDoc,numDoc,numero,fecha,fechaVencimiento,estado);
            ArrayList<CotizacionBusqueda> cotizacionesConsulta = GestionVentas.getInstance().queryCotizacion(cotizacionBusqueda);
            refreshItemsTable2(cotizacionesConsulta);
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void rbPNaturalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPNaturalActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPNaturalActionPerformed

    private void rbPJuridicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPJuridicaActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbPJuridicaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnular;
    private javax.swing.JButton btnBuscar;
    private javax.swing.ButtonGroup btnGpTipoDocumento;
    private javax.swing.ButtonGroup btnGpTipoPersona;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JComboBox cmbEstado;
    private org.jdesktop.swingx.JXDatePicker dpFecha;
    private org.jdesktop.swingx.JXDatePicker dpFechaVen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbDNI;
    private javax.swing.JRadioButton rbPJuridica;
    private javax.swing.JRadioButton rbPNatural;
    private javax.swing.JRadioButton rbRUC;
    private javax.swing.JTable tblCotizaciones;
    private javax.swing.JTextField txtApeMaterno;
    private javax.swing.JTextField txtApePaterno;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtNumCotizacion;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtRazonSocial;
    // End of variables declaration//GEN-END:variables
}
